/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 2/08/2004
 *
 */

package au.com.lastweekend.jim.io.jpeg.write;

import au.com.lastweekend.jim.io.jpeg.AbstractDCTBlock;
import au.com.lastweekend.jim.io.jpeg.MatrixEncodedDCTBlock;
import au.com.lastweekend.jim.io.jpeg.ScanComponent;
import au.com.lastweekend.jim.io.exif.ExifTransform;

/**
 * @author grant@lastweekend.com.au
 * @version $Id: JpegTransform.java,v 1.3 2006/01/28 05:05:47 ggardner Exp $
 */
@SuppressWarnings("PMD.AvoidUsingShortType")
public interface JpegTransform {

    JpegTransform NO_TRANSFORM = new NoTransform();
    JpegTransform ROTATE_CLOCKWISE = new RotateClockwise();
    JpegTransform ROTATE_ANTI_CLOCKWISE = new RotateAntiClockwise();

    Coordinate transform(int width, int height);

    Coordinate reverseTransform(int x, int y, int width, int height);

    int transformZZIndex(int index);

    AbstractDCTBlock transformBlock(ScanComponent component, AbstractDCTBlock sampleBlock);

    ExifTransform getExifTransform();

    /**
     * Structure for holding X/Y parameters used in rotation transforms.
     * 
     * @author grant@lastweekend.com.au
     * @version $Id: JpegTransform.java,v 1.3 2006/01/28 05:05:47 ggardner Exp $
     */
    public final class Coordinate {

        private final int x;
        private final int y;
        private final int width;
        private final int height;

        public Coordinate(int x, int y, int width, int height) {

            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;

        }

        public Coordinate(int width, int height) {

            this(-1, -1, width, height);
        }

        public int getWidth() {

            return width;
        }

        public int getHeight() {

            return height;
        }

        public int getX() {

            return x;
        }

        public int getY() {

            return y;
        }
    }

    /**
     * Default transform does no transformation
     * 
     * @author grant@lastweekend.com.au
     * @version $Id: JpegTransform.java,v 1.3 2006/01/28 05:05:47 ggardner Exp $
     */
    public final class NoTransform implements JpegTransform {

        private NoTransform() {

            super();
        }

        public Coordinate transform(int width, int height) {

            return new Coordinate(width, height);
        }

        public Coordinate reverseTransform(int x, int y, int width, int height) {

            return new Coordinate(x, y, width, height);
        }

        public AbstractDCTBlock transformBlock(ScanComponent component, AbstractDCTBlock sampleBlock) {

            return sampleBlock;
        }

        public int transformZZIndex(int i) {

            return i;
        }

        public ExifTransform getExifTransform() {

            return ExifTransform.NO_TRANSFORM;
        }

    }

    /**
     * Do the X/Y transform thing.
     * 
     * @author grant@lastweekend.com.au
     * @version $Id: JpegTransform.java,v 1.3 2006/01/28 05:05:47 ggardner Exp $
     */
    public abstract class Rotate90 implements JpegTransform {

        public Coordinate transform(int width, int height) {

            // Switch width and height
            return new Coordinate(height, width);
        }

        /*
         * @see au.com.lastweekend.jim.io.jpeg.JpegTransform#transformZZIndex(int) Switch X/Y locations.
         */
        public int transformZZIndex(int i) {

            return AbstractDCTBlock.ZIG_ZAG_ORDER[AbstractDCTBlock.ZIG_ZAG_Y[i]][AbstractDCTBlock.ZIG_ZAG_X[i]];
        }

        public AbstractDCTBlock transformBlock(ScanComponent component, AbstractDCTBlock block) {

            int[][] transformedCoefficients = new int[8][8];
            transformedCoefficients[0][0] = block.getDCCoefficient();
            short[] nonZeroValues = block.getNonZeroValues();
            byte[] indexes = block.getIndexes();

            for (int i = 0; i < indexes.length; i++) {
                int coefficientValue = nonZeroValues[i];
                int x = AbstractDCTBlock.ZIG_ZAG_X[indexes[i]];
                int y = AbstractDCTBlock.ZIG_ZAG_Y[indexes[i]];

                transformCoefficient(x, y, coefficientValue, transformedCoefficients);

            }

            return new MatrixEncodedDCTBlock(transformedCoefficients, 63);

        }

        /**
         * @param x
         * @param y
         * @param coefficientValue
         * @param transformedCoefficients
         */
        protected abstract void transformCoefficient(int x, int y, int coefficientValue, int[][] transformedCoefficients);

    }

    /**
     * 90 degree rotation...
     * 
     * @author grant@lastweekend.com.au
     * @version $Id: JpegTransform.java,v 1.3 2006/01/28 05:05:47 ggardner Exp $
     */
    public final class RotateClockwise extends Rotate90 {

        private RotateClockwise() {

            super();
        }

        public Coordinate reverseTransform(int x, int y, int width, int height) {

            return new Coordinate(y, width - 1 - x, height, width);
        }

        @Override
        protected void transformCoefficient(int x, int y, int coefficientValue, int[][] transformedCoefficients) {

            // negate odd columns and transpose
            transformedCoefficients[y][x] = ((y & 1) == 0) ? coefficientValue : -coefficientValue;
        }

        public ExifTransform getExifTransform() {

            return ExifTransform.ROTATE_CLOCKWISE;
        }

    }

    /**
     * 
     * @author grant@lastweekend.com.au
     * @version $Id: JpegTransform.java,v 1.3 2006/01/28 05:05:47 ggardner Exp $
     */
    public final class RotateAntiClockwise extends Rotate90 {

        private RotateAntiClockwise() {

            super();
        }

        /*
         * @see au.com.lastweekend.jim.io.jpeg.write.JpegTransform#reverseTransform(int, int, int, int)
         */
        public Coordinate reverseTransform(int x, int y, int width, int height) {

            return new Coordinate(height - 1 - y, x, height, width);
        }

        /*
         * @see au.com.lastweekend.jim.io.jpeg.write.JpegTransform.Rotate90#transformCoefficient(int, int, int, int[][])
         */
        @Override
        protected void transformCoefficient(int x, int y, int coefficientValue, int[][] transformedCoefficients) {

            // negate odd rows and transpose
            transformedCoefficients[y][x] = ((x & 1) == 0) ? coefficientValue : -coefficientValue;

        }

        /*
         * @see au.com.lastweekend.jim.io.jpeg.write.JpegTransform#getExifTransform()
         */
        public ExifTransform getExifTransform() {

            return ExifTransform.ROTATE_ANTI_CLOCKWISE;
        }

    }

}